/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ds_semetralka;

import entities.Faculty;
import entities.Person;
import entities.Professor;
import entities.Program;
import entities.Student;
import entities.Subject;
import entities.University;
import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Admin
 */
public class DatabaseCommunication {

    public static EntityManagerFactory emf = Persistence.createEntityManagerFactory("persistence");
    public static EntityManager em = emf.createEntityManager();
    public static EntityTransaction tx = em.getTransaction();

    public static Student getStudent(Long birthNumber) {
        tx.begin();
        Student student = em.find(Student.class, birthNumber);
        tx.commit();
        return student;
    }
    
      public static Person createPerson(String password, String firstName, String lastName) {
          
        Person person = new Person(password, firstName, lastName);
        
        tx.begin();
        em.merge(person);
        tx.commit();
        
        return person;
    }

    public static List<Professor> getProfessor(String name) {
         
        List<Professor> professors;
        
        tx.begin();
        professors = em.createQuery("SELECT c FROM Professor c WHERE c.lastName LIKE ?1").setParameter(1, name).getResultList();
        tx.commit();
        
        return professors;
    }

    public static List<Subject> getSubject(String name) {
        
        List<Subject> subjects;
        
        tx.begin();
        subjects = em.createQuery("SELECT c FROM Subject c WHERE c.name LIKE ?1").setParameter(1, name).getResultList();
        tx.commit();
        
        return subjects;
    }

    public static List<Faculty> getFaculty(String name) {

        List<Faculty> faculties;

        tx.begin();
        faculties = em.createQuery("SELECT c FROM Faculty c WHERE c.name LIKE ?1").setParameter(1, name).getResultList();
        tx.commit();
        
        return faculties;
    }

    public static University getUniversity(Long universityId) {
        tx.begin();
        University university = em.find(University.class, universityId);
        tx.commit();
        return university;
    }
    
    public static Person getPerson(Long personId) {
        tx.begin();
        Person person = em.find(Person.class, personId);
        tx.commit();
        return person;
    }

    public static Program getProgram(Long programId) {
        tx.begin();
        Program program = em.find(Program.class, programId);
        tx.commit();
        return program;
    }

    public static void updateStudent(Student student) {
        tx.begin();
        em.merge(student);
        tx.commit();
    }

    public static void updateSubject(Subject subject) {
        tx.begin();
        em.merge(subject);
        tx.commit();
    }

    public static void updateFaculty(Faculty faculty) {
        tx.begin();
        em.merge(faculty);
        tx.commit();
    }

    public static void updateUniverzity(University university) {
        tx.begin();
        em.merge(university);
        tx.commit();
    }

    public static void updateProgram(Program program) {
        tx.begin();
        em.merge(program);
        tx.commit();
    }
    
    public static void updatePerson(Person person) {
        tx.begin();
        em.merge(person);
        tx.commit();
    }

    public static void deleteStudent(Student student) {
        tx.begin();
        em.remove(student);
        tx.commit();
    }

    public static void deleteSubject(Subject subject) {
        tx.begin();
        em.remove(subject);
        tx.commit();
    }

    public static void deleteFaculty(Faculty faculty) {
        tx.begin();
        em.remove(faculty);
        tx.commit();
    }

    public static void deleteUniverzity(University university) {
        tx.begin();
        em.remove(university);
        tx.commit();
    }

    public static void deleteProgram(Program program) {
        tx.begin();
        em.remove(program);
        tx.commit();
    }
}
